/**
 * JAVACC DEMO 1.0
 */
package com.apache.passport.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.client.LogClient;
import com.apache.passport.common.DesUtils;
import com.apache.passport.common.PassPortConst;
import com.apache.passport.entity.Token;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * description:  用户退出
 *
 * @author Hou Dayu 创建时间：2016-12-18
 */
public class LogoutUserPlugin implements PluginConnector {
    private Logger log = LoggerFactory.getLogger(getClass());
    /**
     * TODO 根据cookie中的value,删除缓存中的key
     *
     * @see PluginConnector#execute(ParamsVo)
     */
    @Override
    public Object execute(ParamsVo paramsvo) throws Exception {
        ResultEntity result = new ResultEntity();
        result.setResult("true");
        String tokenVal = (String) paramsvo.getParams("tokenId");
        String userEname = (String) paramsvo.getParams("userEname");
        if (Validator.isNull(tokenVal)) {
            result.setMessage("缺少必要参数:[tokenId]");
            result.setEntity("F");
            return result;
        }
        Token token = PassPortConst.getToken(tokenVal);
        if (Validator.isEmpty(token)) {
            result.setMessage("证书已失效或不存在");
            result.setEntity("N");
            return result;
        }
        if (Validator.isNull(userEname)) {
            result.setMessage("缺少必要参数:[userEname]");
            result.setEntity("F");
            return result;
        }
        String userEnameEn = DesUtils.getInstance().decrypt(token.getUserEname());
        if (userEname.equals(userEnameEn)) {
            PassPortConst.detToken(tokenVal);
            result.setMessage("用户退出成功");
            result.setEntity("T");
            Map<String, Object> logMap = new HashMap<String, Object>();
            logMap.put("logOperationType", "logout");
            logMap.put("logClassName", "com.apache.uct.common.entity.User");
            logMap.put("logIp", paramsvo.getParams("sysAccreditip"));
            logMap.put("log_userEname", userEname);
            log.info("用户退出 logMap->"+logMap);
            LogClient.getInstance().execute(logMap);//添加日志 未开启审计日志模块
            return result;
        } else {
            result.setMessage("用户名不匹配");
            result.setEntity("F");
            return result;
        }
    }
}
